package shortcircuit.graphics;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;

import javax.swing.JLabel;
import javax.swing.JPanel;

import shortcircuit.GameEngine;

/**
 * This class initiates a PopUpScreen that appears at a specified location in front of the 
 * GameScreen.  The contents of the PopUpScreen are determined by when it appears, either
 * at the beginning or end of a new game.
 * @author Kevin Lewi
 */
public abstract class PopUpScreen extends JPanel
{
	public static int PUZZLE = 0, ENDLESS = 1;
	protected Animator theAnimator;
	public boolean doneLoading;
	protected int stepCounter;
	protected Fader _Fader;
	protected RescaleOp rop;
	protected float _Transparency;
	protected int typeOfGame;
	public boolean beginFading;
	protected Image[] theImages;
	protected GameEngine _Engine;
	
	protected final int NUMBER_IMAGE_OFFSET = 108;
	
	public PopUpScreen(int typeOfGame, GameEngine theEngine)
	{
		_Engine = theEngine;
		this.typeOfGame = typeOfGame;
		beginFading = false;
		doneLoading = false;
		setSize(300, 200);
		setLocation(50, 140);
		setOpaque(false);
		setVisible(true);
		theAnimator = new Animator();
		theAnimator.start();
		this.setIgnoreRepaint(true);
	}
	
	protected void resetROP()
	{
		float[] scales = { 1f, 1f, 1f, _Transparency };
		if(_Transparency < 0.0f)
			scales[3] = 0.0f;
		if(_Transparency > 1.0f){
			scales[3] = 1.0f;
		}
	    float[] offsets = new float[4];
		rop = new RescaleOp(scales, offsets, null);
	}
	
	protected void initiateFader(Image[] theImages)
	{
		beginFading = true;
		doneLoading = true;
		theAnimator = null;
		_Fader = new Fader();
		_Fader.start();
		this.theImages = theImages;
	}
	
	protected void fadeOut(Graphics g)
	{
		_Transparency -= 0.02f;
		if(_Transparency <= 0.0f)
		{
			beginFading = false;
			_Fader = null;
		}
		resetROP();
		Graphics2D g2D = (Graphics2D) g;
		for(int i = 0; i < theImages.length; i++)
		{
			BufferedImage bi;
			try
			{
				bi = new BufferedImage(theImages[i].getWidth(null), theImages[i].getHeight(null), BufferedImage.TYPE_INT_ARGB);
			}
			catch(IllegalArgumentException e) { bi = new BufferedImage(300, 100, BufferedImage.TYPE_INT_ARGB); }
			bi.getGraphics().drawImage(theImages[i], 0, 0, null);
			if(theImages[i] != null)
			{
				
				g2D.drawImage(bi, rop, 0, 0);
				if(i == 0)
					g2D.translate(NUMBER_IMAGE_OFFSET, 0);
				else
					g2D.translate(20, 0);
			}
		}
	}
	
	
	protected class Animator extends Thread
	{
		public void run()
		{
			while(Thread.currentThread() == theAnimator)
			{
				try
				{
					Thread.sleep(1000);
				}
				catch(InterruptedException e) {}
				repaint();
				stepCounter++;
			}
		}
	}
	
	protected class Fader extends Thread
	{
		public void run()
		{
			while(Thread.currentThread() == _Fader)
			{
				try
				{
					Thread.sleep(10);
				}
				catch(InterruptedException e) {}
				repaint();
			}
		}
	}
}
